﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CodeApi.Context;
using CodeApi.Models;

namespace CodeApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BannerController : ControllerBase
    {
        private readonly IndexContext _context;

        public BannerController(IndexContext context)
        {
            _context = context;
        }

        // GET: api/BannerModels
        [HttpGet]
        public async Task<ActionResult<IEnumerable<BannerModel>>> Getbanners()
        {
            return await _context.banners.ToListAsync();
        }

        // GET: api/BannerModels/5
        [HttpGet("{id}")]
        public async Task<ActionResult<BannerModel>> GetBannerModel(int id)
        {
            var bannerModel = await _context.banners.FindAsync(id);

            if (bannerModel == null)
            {
                return NotFound();
            }

            return bannerModel;
        }

        // PUT: api/BannerModels/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutBannerModel(int id, BannerModel bannerModel)
        {
            if (id != bannerModel.ID)
            {
                return BadRequest();
            }

            _context.Entry(bannerModel).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BannerModelExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/BannerModels
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<BannerModel>> PostBannerModel([FromBody]BannerModel bannerModel)
        {
            _context.banners.Add(bannerModel);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetBannerModel", new { id = bannerModel.ID }, bannerModel);
        }

        // DELETE: api/BannerModels/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteBannerModel(int id)
        {
            var bannerModel = await _context.banners.FindAsync(id);
            if (bannerModel == null)
            {
                return NotFound();
            }

            _context.banners.Remove(bannerModel);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool BannerModelExists(int id)
        {
            return _context.banners.Any(e => e.ID == id);
        }
    }
}
